package linuxbox.poll.taglib;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;
import java.io.*;

import linuxbox.poll.logic.*;

public class MyVotes extends BodyTagSupport
{
  private int pollID;
  private int userID;
  private String choiceType;

  public void setPollID(int id)
  {
    pollID = id;
  }
  public void setUserID(int i)
  {
    userID = i;
  }
  public void setChoiceType(String str)
  {
    choiceType = str;
  }

  private boolean checkChoice(Vector v, int choiceID)
  {
    Iterator it = v.iterator();
    while (it.hasNext())
    {
      BoxPollReplyEntry entry = (BoxPollReplyEntry) it.next();
      if ( entry.getChoiceID() == choiceID )
        return true;
    }
    return false;
  }

	public int doStartTag() throws JspException
	{
		try
		{
			JspWriter out = pageContext.getOut();

			BoxPoll home = new BoxPoll();
			Iterator it = home.getChoices(pollID);
			if ( it != null && it.hasNext())
			{
        Vector v = home.getMyVotes(pollID, userID);
			  while (it.hasNext())
			  {
			    BoxPollChoiceEntry entry = (BoxPollChoiceEntry) it.next();
          out.print("<TR><TD width=\"1%\"><FONT size=-1><INPUT type=");
          out.print(choiceType);
          if (checkChoice(v, entry.getID()))
            out.print(" CHECKED");
          out.print(" name=CHOICES value=");
          out.print(entry.getID());
          out.print("> </FONT></TD><TD><FONT size=-1>");
          out.print(entry.getChoice());
          out.print("</FONT></TD></TR>");
			  }
			}
			else
			{
			  out.print("There is no choices setup for this poll!");
			}

			out.flush();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			throw new JspException(e.getMessage());
		}

		return SKIP_BODY;
	}
}
